/*
 * PurchaseController.java
 * Created on 23-Mar-2011, 2:33:19 PM
 */
package pos.desktop.controller;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import pos.control.OrderControl;
import pos.control.PaymentControl;
import pos.control.PurchasedItemControl;
import pos.entity.PaymentDetails;
import pos.exception.POSException;
import pos.ui.screen.Customer;
import pos.entity.Item;
import pos.entity.Order;
import pos.entity.PurchasedItem;
import pos.interfaces.Database;
import pos.storage.StorageFactory;
import pos.util.DisplayMessage;
import pos.desktop.ClientUI;

/**
 * @since 23-Mar-2011, 2:33:19 PM
 * @author Anup Mathew
 * <b>Description:<b> <br> This class is used as the controller
 * for the purchase items use case in the applet application of 
 * the POS. It receives the list of items, calculates its prices
 * after discount, adds the tax, totals the amount, and passes
 * the information to the order and transaction processing
 * backend.
 */
public class PurchaseController {
    //Class variables for one to one mapping
    Customer customerController = new Customer(ClientUI.getConfigFilePath());
    PaymentControl paymentController = new PaymentControl();
    
    /**
     * @since 23-Mar-2011, 2:33:19 PM
     * @param items
     * @return Order
     */
    public Order createOrder(ArrayList<Item> items) {
        Order order = customerController.getOrder(items);
        PurchasedItemControl pic = new PurchasedItemControl();
        pic.updatePurchasedItemPriceAttributes(order.getPurchasedItems());
        
        OrderControl oc = new OrderControl();
        order = oc.updateOrderTotalPrices(order, order.getPurchasedItems());
        return order;
    }

    /**
     * @since 23-Mar-2011, 2:33:19 PM
     * @param order
     * @param paymentDetails
     * @throws POSException
     * @throws SQLException 
     */
    public void processPurchase(Order order, PaymentDetails paymentDetails) throws POSException, SQLException {
        
        Database mysql = (Database) StorageFactory.getStorage(StorageFactory.DBTYPE, Database.MYSQL);
        Connection custConn = null;
        try {
            custConn = mysql.getConnection(Database.CUSTOMER);
        } catch (ClassNotFoundException ex) {
            throw new POSException("Internal application error, we are working on it.");
        }
        
        int returnValue = paymentController.handlePaymentDetails(paymentDetails, order, custConn);
        
        if(returnValue == -1){
            throw new POSException("Error in processing the transaction.");
        }

    } 
    
    /**
     * @since 23-Mar-2011, 2:33:19 PM
     * @param purchasedItem
     * @param item 
     */
    public void calculatePriceAdjustments(PurchasedItem purchasedItem, Item item){
        double discount = item.getDiscount();
             
        double adjustedPrice = (discount/100)*item.getBasePrice();

        double finalPrice = DisplayMessage.roundTwoDecimals(item.getBasePrice() - adjustedPrice);
        
        purchasedItem.setPriceAdjustedAmount(finalPrice);
    }
    
    /**
     * @since 23-Mar-2011, 2:33:19 PM
     * @param item
     * @return double
     */
    public double calculatePriceAdjustments(Item item){
        double discount = item.getDiscount();
             
        double adjustedPrice = (discount/100)*item.getBasePrice();

        double finalPrice = DisplayMessage.roundTwoDecimals(item.getBasePrice() - adjustedPrice);
        
        return finalPrice;
    }
}
